﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

using CustomControlDatabaseInterface.Utilities;

namespace CustomControlDatabaseInterface.ViewModel
{
    class MainWindowViewModel : WorkspaceViewModel
    {

        public WorkspaceViewModel _currentViewModel;

        public WorkspaceViewModel CurrentViewModel
        {
            get
            {
                if (_currentViewModel == null)
                {
                    _currentViewModel = new LoginViewModel();
                }
                return this._currentViewModel;
            }
            set
            {
                if (_currentViewModel == value)
                {
                    return;
                }
                _currentViewModel = value;
                OnPropertyChanged("CurrentViewModel");
            }
        }

        public CustomerDetailsViewModel ActualCustomerDetailsViewModel { get; set; }
        public FilterCustomersViewModel ActualFilterCustomersViewModel { get; set; }
        public ViewCustomersViewModel ActualViewCustomersViewModel { get; set; }
        public FirstViewViewModel ActualFirstViewViewModel { get; set; }
        private WorkspaceViewModel OldViewModel { get; set; }

        public ICommand ShowAdvancedFilterCommand { get; set; }

        public ICommand ShowConfigureDatabaseCommand { get; set; }

        public MainWindowViewModel()
        {
            base.DisplayName = "PSO Database Interface ";

            CurrentViewModel = null;
       
            ActualViewCustomersViewModel = new ViewCustomersViewModel();
            ActualCustomerDetailsViewModel = new CustomerDetailsViewModel();
            ActualFilterCustomersViewModel = new FilterCustomersViewModel();
            ActualFirstViewViewModel = new FirstViewViewModel();
            

            ShowAdvancedFilterCommand = new RelayCommand(param => ShowFilterView());
            ShowConfigureDatabaseCommand = new RelayCommand(param =>ShowConfigureDatabase());

            //Subscribe ViewCustomersViewModel Methods to FilterCustomers Events
            ActualFilterCustomersViewModel.TechnologySelected += ActualViewCustomersViewModel.OnTechnologySelected;
            ActualFilterCustomersViewModel.ControlSelected += ActualViewCustomersViewModel.OnControlSelected;

            //Subscribe CustomerDetails Methods to ViewCustomers Events
            ActualViewCustomersViewModel.ViewDetails += ActualCustomerDetailsViewModel.InititializeFromCustomerName;

            //Subscribe Main Methods to ViewCustomers Events
            ActualViewCustomersViewModel.ViewDetails += OnEditCustomer;

            CurrentViewModel = ActualFirstViewViewModel;
        }

        private void ShowFilterView() {
            CurrentViewModel = ActualFilterCustomersViewModel;
        }

        private void OnEditCustomer(String customerName ) {
            CurrentViewModel = ActualCustomerDetailsViewModel;
        }

        private void ShowConfigureDatabase() {
        
        }


        private void OnViewModelRequestClose(object sender, EventArgs e)
        {
            WorkspaceViewModel workspace = sender as WorkspaceViewModel;
            workspace.Dispose();
            CurrentViewModel = OldViewModel; 
        }


        
    }
}
